#pragma once 
#include<iostream>
#include"Comm.hpp"


class Init
{
public:
   Init()
   {
    umask(0);
    //在这个路径下建立命名管道
    int n=mkfifo(gipefile.c_str(),gmode);
    if(n<0)
    {
        cerr<<"make fifo false"<<endl;
        return;
    }
    cout<<"make fifo sucess"<<endl;
   }
   ~Init()
   { 
     //管道的生命周期是随进程的，所以建一个全局变量，进程结束自己关闭
     int n = unlink(gipefile.c_str());
     if(n<0)
     {
        cerr<<"unlink false"<<endl;
        return;
     }
     cout<<"unlink sucess"<<endl;

   }
};

Init init;
class Server
{
public:
    Server()
    {
    }
    bool OpenPipeForRead()
    {
        _fd=OpenPipe(gForRead);
        if(_fd<0)
        {
            return false;
        }
        return true;

    }
    int RecvPipe(string* out)
    {
        char buffer[gsize];
        ssize_t n = read(_fd,buffer,sizeof(buffer));
        if(n>0)
        {
           buffer[n]=0;
           *out=buffer;
        }
        return n; 
    }
    void ClosePipe()
    {
     ClosePipeHelper(_fd);
    }
    ~Server(){};
private:
    int _fd;
};
